import unittest
import w.base

class DObjectTest(unittest.TestCase):
    def setUp(self):
        self.args = ('a','d')
        self.vargs = {'b':1,'c':2}
        w.base.DObject.ignoredArgs = []
        w.base.DObject.replaceableArgs = []

    def testBase(self):
        d = w.base.DObject(*self.args,**self.vargs)
        ks = d.__dict__.keys()
        for n,v in self.vargs.items():
            self.assert_(n in ks and d.__dict__[n] == v)

    def testIgnored(self):
        w.base.DObject.ignoredArgs.append('b')
        d = w.base.DObject(*self.args,**self.vargs)
        self.assert_('b' not in d.__dict__.keys())

    def testReplaceable(self):
        class new(w.base.DObject): b=True
        new.replaceableArgs.append('b')
        try:
            new(*self.args,**self.vargs)
        except w.base.ReservedArgument,ex:
            self.fail('w.base.ReservedArgument raised')

    def testReplaceableError(self):
        class new(w.base.DObject): b=True
        self.assertRaises(w.base.ReservedArgument,new,*self.args,**self.vargs)

class PrefixDObjectTest(unittest.TestCase):
    def setUp(self):
        self.args = ('a','d')
        self.vargs = {'hb':1,'c':2}
        w.base.PrefixDObject.ignoredArgs = []
        w.base.PrefixDObject.replaceableArgs = []

    def testPrefix(self):
        d = w.base.PrefixDObject(argPrefix='h',*self.args,**self.vargs)
        self.assert_('b' in d.h.__dict__.keys() and d.h.b == self.vargs['hb'])
            
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromNames(('base.DObjectTest','base.PrefixDObjectTest'))
    unittest.TextTestRunner(verbosity=2).run(suite)
